package com.zrrd.learn.chapter07字符串;

/**
 * 字符串习题
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/18 8:04
 */

public class StringExercise01 {

    /**
     * 统计字符串“abc21de416f”中数字个数
     */
    public static void question01(){

        System.out.println("第一题");
        String str = "abc21de416f";
        int count = 0;

        for (int i = 0; i < str.length(); i++) {
            char a = str.charAt(i);
            if (a>='0'&&a<='9'){
                count++;
            }
        }
        System.out.println("字符串中数字的个数为"+count);
    }

    /**
     * 设计一个方法，将给定字符串的正序和反序进行连接 ok—>okko
     */



    public static void question02(String str){

        System.out.println("第二题");
        char[] value=str.toCharArray();
        for(int i=0;i<value.length/2;i++){
            char temp=value[i];
            value[i]=value[value.length-1-i];
            value[value.length-1-i]=temp;
        }

        String newstr = new String(value);
        String laststr = str+newstr;
        System.out.println("新字符串为"+laststr);
    }

    /**
     * 设计一个方法，统计“a”在字符串“this is a test of java”中出现的次数
     */
    public static void question03(String str,char a){

        System.out.println("第三题");
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch==a){
                count++;
            }
        }
        System.out.println("字符串中字符出现的次数为"+count);

    }

    public static void main(String[] args) {

        question01();

        question02("ok");

        question03( "this is a test of java",'a');

    }

}
